<template>
  <div >
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">考试信息</el-breadcrumb-item>
      <el-breadcrumb-item>添加考试信息</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <div style="width: 500px;margin: 0 auto">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="标题" prop="parentId">
        <el-input v-model="ruleForm.title" placeholder="请输入考试信息标题"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="name">
        <el-input v-model="ruleForm.description"
                  placeholder="请输入描述内容"></el-input>
      </el-form-item>
      <el-form-item label="规则" prop="keywords">
        <el-input v-model="ruleForm.rule"
                  placeholder="请输入考试规则内容"></el-input>
      </el-form-item>
      <el-form-item label="信息内容" prop="icon">
        <el-input v-model="ruleForm.examContent"
                  type="textarea"
                  :autosize="{ minRows: 5, maxRows: 8}"
                  placeholder="请输入信息内容"></el-input>
      </el-form-item>
      <el-form-item label="考试等级" prop="sort">
        <el-input v-model="ruleForm.grade"></el-input>
      </el-form-item>
      <el-form-item label="考试证书" prop="sort">
        <el-input v-model="ruleForm.certificate"></el-input>
      </el-form-item>
      <el-form-item >
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
  </div>
</template>
<style>
</style>
<script>
export default {
  data() {
    return {
      ruleForm: {
        title:'',
        description:'',
        rule:'',
        examContent:'',
        grade:'',
        certificate:''
      },
      rules: {
        /*trigger: 'blur'触发方式,丢失焦点*/
        // parentId: [
        //   { required: true, message: '请输入父级类别的ID', trigger: 'blur' },
        //   { pattern:  , message: '父级类别的ID必须是数字', trigger: 'blur' }
        // ],
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          /*向后端提交异步请求*/
          let  url="http://120.77.154.166:8080/examIntroduction/add-new"
          let formData = this.qs.stringify(this.ruleForm);
          // console.log('formData = ' + formData);
          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url,formData).then((response)=>{
            let jsonResult = response.data;
            if (jsonResult.state == 20000) {
              this.$message({
                message: '添加类别成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$alert(jsonResult.message, '操作失败', {
                confirmButtonText: '确定',
                callback: action => {}
              });
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>
